﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using RBOSS.Business.CreditCard;
using RBOSS.Business.CustomerModule;
using System.Linq;
using RBOSS.Utilities;

namespace RBOSS.WebUI
{
    public partial class CustomerProfile : System.Web.UI.Page
    {
        private readonly CustomerSubSystem _customerSubSystem = new CustomerSubSystem();
        private readonly ICreditCardSubsystem _creditCardSubsystem = new CreditCardSubsystem();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["role"] != null && (string) Session["role"] == "Customer" && Session["email"] != null)
            {
                var customerInfo = _customerSubSystem.GetCustomerInformation((string) Session["email"]);
                lblEmail.Text = customerInfo.Email;
                lblFirstName.Text = customerInfo.FirstName;
                lblLastName.Text = customerInfo.LastName;
                lblPhoneNumber.Text = customerInfo.PhoneNumber;
                lblPostalCode.Text = customerInfo.PostalCode;
                lblProvience.Text = customerInfo.Provience;
                lblCity.Text = customerInfo.City;
                lblCountry.Text = customerInfo.Country;

                FillOrderHistoryGridView(customerInfo.CustomerId);
                FillCreditCardGridView(customerInfo.CustomerId);

                ViewState["CustomerId"] = customerInfo.CustomerId;

            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            gvCreditCards.SelectedIndexChanged += DeleteCreditCardClick;
        }

        private void DeleteCreditCardClick(object sender, EventArgs e)
        {
            if(gvCreditCards.SelectedDataKey == null)
                return;
            var dataKey = gvCreditCards.DataKeys[gvCreditCards.SelectedIndex];
            if (dataKey != null)
            {
                var id = (int) dataKey.Value;
                _creditCardSubsystem.DeleteCreditCard(id);
            }
            FillCreditCardGridView(Convert.ToInt32(ViewState["CustomerId"]));
        }

        private void FillOrderHistoryGridView(int customerId)
        {
            var customerOrders = _customerSubSystem.GetCustomerOrders(customerId);
            
            gbOrderHistory.DataSource = customerOrders;
            gbOrderHistory.DataBind();
        }

        private void FillCreditCardGridView(int customerId)
        {
            var creditCards = _creditCardSubsystem.GetValidCustomerCreditCards(customerId);
            foreach (var creditCard in creditCards)
            {
                creditCard.CreditCardNumber =
                    DecryptCredtiCardNumberAndSendLastFourCharacters(creditCard.CreditCardNumber);
            }
            gvCreditCards.DataSource = creditCards;
            gvCreditCards.DataBind();
        }


        private string DecryptCredtiCardNumberAndSendLastFourCharacters(string encryptedCardNumber)
        {
            var decryptedCardNumber = RBOSSecurity.Decrypt(encryptedCardNumber);
            return "************" + decryptedCardNumber.Substring(Math.Max(0, decryptedCardNumber.Length - 4));
        }
    }
}